<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>资金到位</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/icon.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/buttons.css" />
<link rel="stylesheet" type="text/css" href="/jxcsxm/js/autocomplete/jquery.autocomplete.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-form.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/datagrid-detailview.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/util/jquery.cookie.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/YMLib.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/YWLib.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/autocomplete/jquery.autocomplete.js" ></script>
<style type="text/css">.table{border: 1px solid #FFE7BA;} .table tr{border: 1px solid #FFE7BA;} .table tr td{border: 1px solid #FFE7BA;}</style>
<script type="text/javascript">
function checkCf(id){
	var name=$(id).val();
	$.ajax({
			url:'/jxcsxm/tjbb/checkcgsNameCf.do',
			data:"cgsjhlr.xmmc="+name,
			type:'post',
			dataType:'json',
			success:function(msg){
				if(msg){
					alert("项目名称重复，请重新填写");
					$(id).val('');
					$(id).focus();
				    
				}
			}
		})
}
function yzsz(id){
	if(isNaN(Number($(id).val()))){  
		//当输入不是数字的时候，Number后返回的值是NaN;然后用isNaN判断。
        alert('请填入数字！');
        $(id).val('');
		$(id).focus();
    }
}
function bcsqbfje(id){
	if(isNaN(Number($(id).val()))){  
		//当输入不是数字的时候，Number后返回的值是NaN;然后用isNaN判断。
        alert('请填入数字！');
        $(id).val('');
		$(id).focus();
    }
	var bcjenum = Number($("#bcje").val());
	var zhjhjetotal = Number($("#zhjhjetotal").val());
	var zhjhje = Number($("#zhjhje").val());
	var jhje = Number($("#jhje").val());
	if(bcjenum>zhjhje){
		alert("你申请金额不能大于计划金额");
		$(id).val('');
		$(id).focus();
		return;
	}else if( zhjhje>jhje){
		alert("用户计划金额不能大于计划金额");
		$("#zhjhje").val('');
		$("#zhjhje").focus();
		return;
	}
	else if(bcjenum>zhjhjetotal){
		if($("#zhjhjetotal").val()!=""){
			alert("你的累计申请金额超过用户计划金额,此用户剩余金额为:"+zhjhjetotal);
			$(id).val('');
			$(id).focus();
			return;
		}
	}
}
function yhjhje(id){
	if(isNaN(Number($(id).val()))){  
		//当输入不是数字的时候，Number后返回的值是NaN;然后用isNaN判断。
        alert('请填入数字！');
        $(id).val('');
		$(id).focus();
    }
	//jhjetotal
	var xmjhje = $(id).val();
	var numm = Number($("#jhjetotal").val());
	var bcje = Number($("#bcje").val());
	var jhje = Number($("#jhje").val());
	if($("#jhjetotal").val()!=""){
		if(xmjhje>numm){
			alert("你申请的用户金额超过项目的计划金额，请重新填写！");
			$(id).val('');
			$(id).focus();
		}
	}else if( xmjhje<bcje  ){
		alert("本次申请金额不能大于用户计划金额");
		$("#zhjhje").val('');
		$("#zhjhje").focus();
		return;
	}else if( xmjhje>jhje){
		alert("用户计划金额不能大于计划金额");
		$("#zhjhje").val('');
		$("#zhjhje").focus();
		return;
	}
}
function autoCompleteXmmc(){
	$("#xmmc").autocomplete("/jxcsxm/tjbb/autoCgsxmmc.do", {
		multiple : false,
		minChars :1,
		multipleSeparator : ' ',
		mustMatch: true,
  		cacheLength : 0,
  		delay : 0,
  		max : 150,
  		extraParams : {
  				"cgsjhlr.xmmc":function() {
  				var d = $("#xmmc").val();
  				return d;
  			}
  		},
  		dataType : 'json',// 返回类型
  		// 对返回的json对象进行解析函数，函数返回一个数组
  		parse : function(data) {
  			var aa = [];
  			aa = $.map(eval(data), function(row) {
  					return {
  						data : row,
  						value : row.xmmc.replace(/(\s*$)/g,""),
  						result : row.xmmc.replace(/(\s*$)/g,"")
  					};
  				});
  			return aa;
  		},formatItem : function(row, i, max) {
  			return row.xmmc.replace(/(\s*$)/g,"");
  		}
  	}).result(function(e, item) {
  		$("#twh1").val(item.twh.substring(0,item.twh.indexOf("[")));
  		$("#twh2").val(item.twh.substring(item.twh.indexOf("[")+1,item.twh.indexOf("]")));
  		$("#twh3").val(item.twh.substring(item.twh.indexOf("]")+1,item.twh.indexOf("号")));
  		$("#jwh1").val(item.jwh.substring(0,item.jwh.indexOf("[")));
  		$("#jwh2").val(item.jwh.substring(item.jwh.indexOf("[")+1,item.jwh.indexOf("]")));
  		$("#jwh3").val(item.jwh.substring(item.jwh.indexOf("]")+1,item.jwh.indexOf("号")));
  		$("#jhje").val(item.jhje);
  		if(item.id==null||item.id==""){
  			$("#id").val(newGuid());
  			queryysqlist($("#id").val());
  			sumye($("#id").val());
  		}else{
  			$("#id").val(item.id);
  			sumye(item.id);
  			queryysqlist(item.id);
  			
  		}
  		//$("#name").val(item.truename)
	});
}
//var jhxdwh=$("#wh1").textbox('getValue')+"["+$("#wh2").textbox('getValue')+"]"+$("#wh3").textbox('getValue')+"号";
	
function autoCompleteHm(){
	$("#hm").autocomplete("/jxcsxm/tjbb/autohm.do", {
		multiple : false,
		minChars :0,
		multipleSeparator : ' ',
		mustMatch: true,
  		cacheLength : 0,
  		delay : 0,
  		max : 150,
  		extraParams : {
  				"xmjbxx.hm":function() {
  				var d = $.cookie("unit2").substring(0,1);
  				return d;
  			}
  		},
  		dataType : 'json',// 返回类型
  		// 对返回的json对象进行解析函数，函数返回一个数组
  		parse : function(data) {
  			var aa = [];
  			aa = $.map(eval(data), function(row) {
  					return {
  						data : row,
  						value : row.hm.replace(/(\s*$)/g,"")+"("+row.khh.replace(/(\s*$)/g,"")+"        "+row.zh.replace(/(\s*$)/g,"")+")",
  						result : row.hm.replace(/(\s*$)/g,"")+"("+row.khh.replace(/(\s*$)/g,"")+"        "+row.zh.replace(/(\s*$)/g,"")+")"
  					};
  				});
  			return aa;
  		},formatItem : function(row, i, max) {
  			return row.hm.replace(/(\s*$)/g,"")+"("+row.khh.replace(/(\s*$)/g,"")+"        "+row.zh.replace(/(\s*$)/g,"")+")";
  		}
  	}).result(function(e, item) {
		$("#zhid").val(item.id);
		$("#zhjhje").val("");
		$("#syzhjhje").html("");
		$("#zhjhjetotal").val("");
		$.ajax({
  			url:'/jxcsxm/tjbb/selectsbzjByzhid.do',
  			data:"xmjbxx.jhid="+$("#id").val()+"&xmjbxx.zhid="+item.id,
  			type:'post',
  			dataType:'json',
  			success:function(msg){
  				if($("#jhje").val()<$("#syxmjhje").val()){
		        	alert("项目计划金额已经申请完毕！不能在添加用户了！");
		        	$("#hm").val("");
		        }else{
		  			if(msg!=null&&msg!=""){
		  				$('#zhjhje').attr("disabled",true);
		  				$("#zhjhje").val(msg.zhjhje);
		  				var nums =msg.zhjhje-msg.bcje;
		  				$("#zhjhjetotal").val(nums);
		  				$("#syzhjhje").html("剩余资金:["+nums+"]万元");
		  			}else{
		  				$('#zhjhje').attr("disabled",false);
		  			}
		        }
  			}
  		})
  	})		
}


$(function(){
	//autoCompleteXmmc();
	autoCompleteHm();
	//loadBmbm('bd1','标段');
	loadBmbm('xmnf','项目年份',new Date().getFullYear());
	loadBmbm('xmlx','国省道项目类型');
	var yf=new Date().getMonth()+1;
	var day=new Date().getDate();
	if(yf<10) yf='0'+yf;
	if(day<10) day='0'+day;
	
	$("#sqrq1").datebox('setValue',new Date().getFullYear()+"-"+yf+"-"+day);
	$('#zhjhje').attr("disabled",true);
})
function loadxmlx(){
	var xmdj=$("#xmdj").val();
	if(xmdj=="国省道"){
		loadBmbm('xmlx','国省道项目类型');
	}else{
		loadBmbm('xmlx','农村项目类型');
	}
}
function zjdwtj(){
	//if($('#bd1').combo("getValue")==""){alert("请选择标段");return;}
	$('#zhjhje').attr("disabled",false);
	if($("#id").val()==null||$("#id").val()=='')
	$("#id").val(newGuid());
	$('#sqrq').val($('#sqrq1').datebox("getValue"));
	var twh=$("#twh1").val()+"["+$("#twh2").val()+"]"+$("#twh3").val()+"号";
	var jwh=$("#jwh1").val()+"["+$("#jwh2").val()+"]"+$("#jwh3").val()+"号";
	$("#twh").val(twh);
	$("#jwh").val(jwh);
	if($("#xmmc").val()==''||$("#xmmc").val()==null){
		alert("项目名称不能为空");
		return;
	}
	if($("#jhje").val()==''||$("#jhje").val()==null){
		alert("计划金额不能为空");
		return;
	}
	//alert($('#fileupload').form('validate'));
	//alert($('#fileupload').val());
	if($('#fileupload').val()==''||$("#fileupload").val()==null){
		 alert("上传文件不能为空");
		 return;
		 } else {
		  var filepath = $("#fileupload").val();
          var suffix = filepath.substr(filepath.lastIndexOf("."));
          //alert(suffix);
		  if(".xls" != suffix && ".xlsx" != suffix ){
			alert("您选择的上传文件不是有效xls或者xlsx文件！");
		    return ;
		  }
 }
	/* $.ajax({
		type:'post',
		url:'/jxcsxm/jhcx/getJhxdByWh.do',
		data:"xmjbxx.jhxdwh="+jhxdwh+"&xmjbxx.xmbm="+xmbm,
		dataType:'json',
		async:false,
		success:function(msg){
            if(msg!=null){
                if(parseFloat(msg.ztz)<parseFloat(accAdd(ztz,dwzj))){
                    dyjhz=true;
                }
                if(parseFloat(msg.xdzbz)<parseFloat(accAdd(zbz,dwzbz))){
                    dyjhb=true;
                }
            }
			
		}
	}); */

		loadjzt();
		$('#submit').ajaxSubmit({
			dataType:'json',
			success:function(msg){
				if(msg){
					disLoadjzt();
					console.log(msg);
					alert("保存成功！");
					//zjdwxz();
					//sumye($("#id").val());
					queryysqlist($("#id").val());
					// parent.$("#ysqlb").datagrid('reload');
					// closeWindow();
				}else{
					disLoadjzt();
					console.log(msg);
					alert("保存失败！"+msg.responseText);
				}
			},
			error:function(msg){
				disLoadjzt();
				//console.log(msg);
				alert("保存失败！"+msg.responseText);
			}
		});
	
	
}
function zjdwxz(){
	$("#hm").val("");
	$("#bcje").val("");
	$("#zhjhje").val("");
	$("#syzhjhje").html("");
	$("#zhjhjetotal").val("");
	alert("请重新选择用户！");
}
function sumye(id){
	$.ajax({
		url:'/jxcsxm/tjbb/sumye.do',
		data:"xmjbxx.id="+id,
		type:'post',
		dataType:'json',
		success:function(msg){
			var nums = (accSub($("#jhje").val(),msg.name));
			$("#jhjetotal").val(nums);
			$("#syxmjhje").html("剩余资金:["+nums+"]万元");
		}
			
	})
}

function delsqmx(id){
	if(confirm("您确认删除吗？"))
	$.ajax({
		url:'/jxcsxm/tjbb/delsqmx.do',
		data:"xmjbxx.id="+id,
		type:'post',
		dataType:'json',
		success:function(msg){
			alert("删除成功")
			$("#ysqlb").datagrid('reload');
		}
			
	})
	
	
}



function queryysqlist(id){
	var params={'cgsjhlrMx.jhid':id};
	var col;
	col=[[
	{field:'cz',title:'操作',align:'center',
		formatter: function(value,row,index){
			var result='<a style="color:#3399CC;text-decoration:none;" href="javascript:delsqmx('+"'"+row.id+"'"+')" >删除</a>';
			result+='<a style="text-decoration:none;color:#3399CC;" href=javascript:editsqXm("'+index+'")>编辑</a>&nbsp;&nbsp;'
			return result; 
		}
	},
	{field:'sqrq',title:'申请日期',width:175,align:'center'},
	{field:'zhjhje',title:'用户计划金额(万元)',width:100,align:'center'},
	{field:'bcje',title:'申请金额(万元)',width:100,align:'center'},
	{field:'hm',title:'户名',width:175,align:'center'},
	{field:'khh',title:'开户行',width:225,align:'center'},
	{field:'zh',title:'账号',width:225,align:'center'},
	{field:'xmlx',title:'项目类型',width:100,align:'center'}
	
]];
	$('#ysqlb').datagrid({    
	    url:'/jxcsxm/tjbb/queryCgslist.do',
	    striped:true,
	    pagination:true,
	    rownumbers:true,
	    pageNumber:1,
	    pageSize:10,
	    checkOnSelect:true,
	    
	    queryParams: params,
	    columns:col
	}); 
}
function editsqXm(index){
	YMLib.Var.data=$('#ysqlb').datagrid('getRows')[index];
	YMLib.UI.createWindow('mywin',"编辑","sbzjqklrsqXm_bj.jsp",'mywin','600','300');	
}
</script>
</head>
<body>

<form id="submit" action="/jxcsxm/tjbb/insertSbzjCgsjhlr.do" method="post" enctype="multipart/form-data">
<table class='table' style="width: 100%; background-color: #FFE7BA; font-size: 12px"
			border="0" cellpadding="3" cellspacing="1">
			
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right"  width="140px;">
				项目名称：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left" colspan="3">
					<input type="text" name="cgsjhlr.xmmc" id="xmmc" style="width: 700px" onchange="checkCf(this)"/>
					<input type="hidden" name="cgsjhlr.id" id="id"  />
					</td>
				<!-- <td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				国债：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
					<input type="text" name="gz" id="gz" onchange="yzsz(this)" style="width: 120px" />万元</td>
				 -->
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right" width="140px;">
				厅文号：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
					<input type="text" name="twh1" id="twh1" style="width: 120px" />[<input type="text" name="twh2" id="twh2" style="width: 50px" />]<input type="text" name="twh3" id="twh3" style="width: 30px" />号
					<input type="hidden" name="cgsjhlr.twh" id="twh"  />
					</td>
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right" width="140px;">
				局文号：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
					<input type="text" name="jwh1" id="jwh1" style="width: 120px" />[<input type="text" name="jwh2" id="jwh2" style="width: 50px" />]<input type="text" name="jwh3" id="jwh3" style="width: 30px" />号
					<input type="hidden" name="cgsjhlr.jwh" id="jwh"  />
					</td>
				
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				项目年份：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
				<select id="xmnf" name="cgsjhlr.xmnf" style="width: 80px;"></select></td>
				   
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				申请日期：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
					<input type="text" class='easyui-datebox' name="sqrq1" id="sqrq1" style="width: 124px" />
				 	<input type="hidden" name="cgsjhlr.sqrq" id="sqrq" style="width: 124px" /></td>
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				项目计划金额：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
				    <input type="hidden" name="jhjetotal" id="jhjetotal"/>
					<input type="text" name="cgsjhlr.jhje" id="jhje" onchange="yzsz(this)" style="width: 120px" />万元<span id="syxmjhje" style="color:red;"></span></td>
				
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				项目等级：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
				<select id="xmdj" name="cgsjhlr.xmdj" style="width: 80px;" onchange="loadxmlx()">
					<option value="国省道">国省道</option>
					<option value="农村公路">农村公路</option>
				</select></td>
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				备注：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left" colspan="3">
					<input type="text" name="cgsjhlr.bz" id="bz" style="width: 620px" /></td>
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">选择文件：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left" colspan="3">
					<input type="file" name="fileupload" id="fileupload"   style="font-weight: bold;" align="right"  />
				</td>
<!-- 					<input type="submit" value="文件上传" id='upFile-btn' font-weight: bold;" align="right">
				<td style="background-color: #ffffff; height: 20px;" align="left" colspan="3">
					<input type="text" name="hm" id="hm"  style="width: 620px" />
					<input type="hidden" name="xmjbxx.zhid" id="zhid"   /></td> -->
<%--   				<form id="file_form" action="UpdFile" enctype="multipart/form-data" method="post">
					<input type="file" name="file" id="file_input" />	
        			<input type="submit" value="文件上传" id='upFile-btn'>
    			</form>
  			<td> 
     			<input type="button" id="uploadFile" class="ldBtnGray" value="选择文件" />&nbsp;&nbsp;
     			<a href="${basePath}/upload/template/template13.xlsx" download="template13.xlsx">
        		 <span style='float: right; margin-right: 20px; margin-top: 10px; font-size: 18px; color: #2891d2;'>模板下载</span>
    			 </a>
				</td>  --%>
			</tr>
			<!-- <tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				项目类型：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
				    <select id="xmlx" name="xmjbxx.xmlx" style="width: 100px;"></select>
                </td>
				
			</tr>
			<tr style="height: 35px;">
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				用户计划金额：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
				    <input type="hidden" name="zhjhjetotal" id="zhjhjetotal"/>
					<input type="text" name="xmjbxx.zhjhje" id="zhjhje" onchange="yhjhje(this)" style="width: 120px" disabled=disabled/>万元,<span id="syzhjhje" style="color:red;"></span></td>
				<td style="background-color:#FFEFD5;color: #007DB3; font-weight: bold;" align="right">
				本次申请拨付金额：</td>
				<td style="background-color: #ffffff; height: 20px;" align="left">
					<input type="text" name="xmjbxx.bcje" id="bcje" onchange="bcsqbfje(this)" style="width: 120px" />万元</td>
			</tr> -->
			
			<tr style="height: 35px;">
				<td colspan="4" style="background-color: #ffffff;"align="center">
<!-- 				<a id='mybuttion3' style="margin-left: 5px;margin-bottom: 1px;" href="javascript:zjdwxz()" onmouseover="szgq('button button-tiny button-glow button-rounded button-raised button-primary','mybuttion3')" onmouseout="szgq('button button-tiny button-rounded button-raised button-primary','mybuttion3')"  class="button button-tiny button-rounded button-raised button-primary">新增</a> -->
				<a id='mybuttion1' style="margin-left: 5px;margin-bottom: 1px;" href="javascript:zjdwtj()" onmouseover="szgq('button button-tiny button-glow button-rounded button-raised button-primary','mybuttion1')" onmouseout="szgq('button button-tiny button-rounded button-raised button-primary','mybuttion1')"  class="button button-tiny button-rounded button-raised button-primary">保存</a>
				<a id='mybuttion2' style="margin-left: 5px;margin-bottom: 1px;" href="javascript:closeWindow()" onmouseover="szgq('button button-tiny button-glow button-rounded button-raised button-primary','mybuttion2')" onmouseout="szgq('button button-tiny button-rounded button-raised button-primary','mybuttion2')"  class="button button-tiny button-rounded button-raised button-primary">取消</a>
			
			</tr>
			<tr style="height: 35px;">
				<td colspan="4" style="background-color: #ffffff;"align="center">
						已申请列表
				</td>
				
			</tr>
			<tr style="height: 35px;" >
				<td colspan="4" style="background-color: #ffffff;"align="center">
				<table  id='ysqlb'>
				</table>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>